package com.troy.xifan.activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.widget.Toolbar; import android.text.TextUtils; import android.view.KeyEvent; import android.view.MenuItem; import android.webkit.WebChromeClient; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import butterknife.BindView; import butterknife.ButterKnife; import com.chenenyu.router.annotation.Route; import com.troy.xifan.R; import com.troy.xifan.config.Constants; /** * Created by chenlongfei on 2017/2/17. */ @Route({ Constants.Router.BROWSER, Constants.Router.SCHEME + Constants.Router.BROWSER }) public class BrowserActivity extends BaseActivity { public static final String BUNDLE_URL = "url"; @BindView(R.id.toolbar) Toolbar mToolbar; @BindView(R.id.webview) WebView mWebView; private String mUrl; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_borwser); ButterKnife.bind(this); Bundle bundle = getIntent().getExtras(); if (bundle != null) { mUrl = bundle.getString(BUNDLE_URL); } initViews(); } @Override protected void initViews() { setSupportActionBar(mToolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { view.loadUrl(request.getUrl().toString()); return true; } }); mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onReceivedTitle(WebView view, String title) { mToolbar.setTitle(title); } }); if (!TextUtils.isEmpty(mUrl)) { mWebView.loadUrl(mUrl); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); break; } return super.onOptionsItemSelected(item); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (mWebView.canGoBack()) { mWebView.goBack(); return true; } else { finish(); } } return super.onKeyDown(keyCode, event); } }